package com.text.mqtt_text.mqtt;

// dashscope SDK的版本 >= 2.18.2
import java.io.IOException;
import java.lang.System;
//sk-760961e7842c480997d16f36c559acac
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.text.mqtt_text.pojo.Memory;
import com.text.mqtt_text.service.StudyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Main {


    public static GenerationResult callWithMessage(Memory memory) throws ApiException, NoApiKeyException, InputRequiredException, IOException {

//        Path path = Paths.get("E:\\JAVA\\javaproject\\study_mqtt_text\\downloaded_configs\\memory.txt");
//        String data = Files.readString(path);
        String data = memory.getSummaryMemory();
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("你现在是一个儿童情感分析大师，我会给你发送一段话，是关于孩子和AI的对话总结，请你根据文本信息返回三百字以内的以json格式为标准的字符串数据，属性有三个{“analysis”：“情感分析内容”和“suggestion”：“对家长的建议”和“variation”：“情感变化”}情感变化内容只要五个数字，数字范围为0-10，表示情绪好坏的变化，数字之间用逗号隔开，不需要任何说明")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(data)
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量，请用阿里云百炼API Key将下行替换为：.apiKey("sk-xxx")
                .apiKey("sk-760961e7842c480997d16f36c559acac")
                // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
    public static String thinks(Memory memory) {
        try {
            GenerationResult result = callWithMessage(memory);
            log.info("返回的结果：{}",result.toString());
            String thinks = result.getOutput().getChoices().getFirst().getMessage().getContent();
            log.info("第一个结果：{}",thinks);
            return thinks;
        } catch (ApiException | NoApiKeyException | InputRequiredException | IOException e) {
            System.err.println("错误信息："+e.getMessage());
            System.out.println("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
        }
        System.exit(0);
        return null;
    }
}